@extends('admin.layouts.app')
@inject('menus','App\Repositories\Presenter\MenuPresenter')
@section('css')
<link href="/admins/vendors/select2/dist/css/select2.min.css" rel="stylesheet">
<link href="/admins/css/menu.css" rel="stylesheet">
@endsection

@section('content')
<div class="">
	<div class="page-title">
		<div class="title_left">
			<h3>菜单管理</h3>
		</div>
	</div>

	<div class="clearfix"></div>
	@include('flash::message')
	<div class="row">
		<div class="col-md-6 col-sm-12 col-xs-12">
			<div class="x_panel">
				<div class="x_title">
					<h2>现有菜单</h2>
					<div class="clearfix"></div>
				</div>
				<div class="x_content">
					<div class="dd" id="nestable3">
						{!! $menus->getMenuList($menulist) !!}
					</div>
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-12 col-xs-12">
			<div class="x_panel">
				<div class="x_title">
					<h2>添加</h2>
					<div class="clearfix"></div>
				</div>
				<div class="x_content">
					<form id="menuForm" action="{{url('admin/menu')}}" method="post" data-parsley-validate class="form-horizontal form-label-left">
						{{ csrf_field() }}
						<div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}">
							<label class="control-label col-md-3 col-sm-3 col-xs-12">名称<span class="required">*</span> </label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<input type="text" class="form-control"  name = "name" value="{{ old('name') }}" placeholder="菜单名称">
								@if ($errors->has('name'))
								<span class="help-block"> <strong>{{ $errors->first('name') }}</strong> </span>
								@endif
							</div>
						</div>
						<div class="form-group {{ $errors->has('pid') ? ' has-error' : '' }}">
							<label class="control-label col-md-3 col-sm-3 col-xs-12">父菜单<span class="required">*</span></label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<select class="select2_single form-control" tabindex="-1" name="pid">
									{!! $menus->getMenu($pmenu,old('pid')) !!}
								</select>
								@if ($errors->has('pid'))
								<span class="help-block"> <strong>{{ $errors->first('pid') }}</strong> </span>
								@endif
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >图标</label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<input type="text" class="form-control"  name = "icon" value="{{ old('icom') }}" placeholder="菜单图标">
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >是否为链接</label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<select class="form-control" tabindex="-1" name="is_url">
									<option value="0" @if (old('is_url') =="0") selected @endif>否</option>
									<option value="1" @if (old('is_url') =="1") selected @endif>是</option>
								</select>
							</div>
						</div>
						<div class="form-group {{ $errors->has('permission') ? ' has-error' : '' }}">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >权限<span class="required">*</span></label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<input type="text" class="form-control"  name = "permission" value="{{ old('permission') }}" placeholder="权限">
								@if ($errors->has('permission'))
								<span class="help-block"> <strong>{{ $errors->first('permission') }}</strong> </span>
								@endif
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >是否显示</label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<select class="form-control" tabindex="-1" name="is_show">
									<option value="1" @if (old('is_show') =="1") selected @endif>显示</option>
									<option value="0" @if (old('is_show') =="0") selected @endif>隐藏</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >备注</label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<input type="text" class="form-control"  name = "remark" value="{{ old('remark') }}" placeholder="菜单备注">
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" >排序</label>
							<div class="col-md-9 col-sm-9 col-xs-12">
								<input type="text" class="form-control" name = "sort" value="{{ old('sort')?old('sort'):0 }}" placeholder="菜单排序">
							</div>
						</div>
						<div class="ln_solid"></div>
						<div class="form-group">
							
							<div class="col-md-12 col-sm-12 col-xs-12 text-center">
                        			<a href="{{route('menu.index')}}" type="submit" class="btn btn-primary">取消</a>
                        			<button type="submit" class="btn btn-success">提交</button>
                        		</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
@endsection

@section('js')
<script src="/admins/vendors/select2/dist/js/select2.full.min.js"></script>
<script src="/admins/vendors/nestable/jquery.nestable.js"></script>
<script type="text/javascript">
	$(function() {
		//nestable
		$('.dd').nestable({ maxDepth: {{config('admins.globals.menu_level')}} });
	
		//select2
		var select2 = $(".select2_single").select2({
			placeholder: "请选择",
			allowClear: true
		});
		//修改
		$("a.editMenu").click(function(){
			//查询
			var _url = $(this).attr('data-href');
		    	$.ajax({
		        url:_url,
		        dataType:'json',
		        beforeSend:function() {
			        	// loading
			        	layer.load(1);
		        },
		        success:function(menu) {
		        		// 关闭loading
			        	layer.closeAll('loading');
			        	if (menu.status) {
			        		setEditVal(menu,select2);
			        	}
					layer.msg(menu.msg);
		        },
		        error:function(){
		        		layer.closeAll('loading');
		        		alert("xxx");
		        }
		    });
		});
		
		//删除
		$("a.destoryMenu").click(function(){
	    		var _delete = $(this).attr('data-id');
	    		//询问框
			layer.confirm('确定要删除菜单？', {
				btn: ['确定','取消'] //按钮
			}, function(){
				$('form[name=delete_item_'+_delete+']').submit();
			});
	    });
		
	});
	
	function setEditVal(menu,select2) {
		$('input[name=name]').val(menu.name);
		select2.val(menu.pid).trigger("change");
 		$('input[name=icon]').val(menu.icon);
		$('select[name=is_url]').val(menu.is_url).trigger("change");
		$('input[name=permission] ').val(menu.permission);
		$('select[name=is_show]').val(menu.is_show).trigger("change");
		$('input[name=remark]').val(menu.remark);
		$('input[name=sort]').val(menu.sort);
		
		$('#menuForm').attr('action',menu.update);
		var _method = $('#method');
		if (_method.length < 1) {
			$('#menuForm').append('<input type="hidden" id="method" name="_method" value="PATCH">');
		}
		// 判断表单是否存在相关数据
		var _id = $('input[name=id]');
		if (_id.length > 0) {
			_id.val(menu.id);
		}else{
			$('#menuForm').append('<input type="hidden" name="id" value="'+menu.id+'">');
		}
	}
	
</script>
@endsection
